<template>
    <div>
        <div class="a0">
            <div class="left">
                <div class="a1" @click="toData1">首页</div>
                <div class="a1">学生管理</div>
                <div class="a1" @click="exit">退出登陆</div>
                <div class="a1">菜单</div>
                <div class="a1">菜单</div>
                <div class="a1">菜单</div>
                <div class="a1">菜单</div>
            </div>
            <div class="right">
                <div class="head">
                    <!-- 新增按钮 -->
                    <el-button type="primary" size="small" @click="dialogFormVisible = true">新增</el-button>
                </div>
                <div class="body">
                    <el-table :data="students" border style="width: 100%">
                        <el-table-column prop="id" label="ID" width="180"></el-table-column>
                        <el-table-column prop="sno" label="学号" width="180"></el-table-column>
                        <el-table-column prop="sname" label="姓名" width="180"></el-table-column>
                        <el-table-column prop="sage" label="年龄" width="180"></el-table-column>
                        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
                        <el-table-column prop="updateTime" label="更新时间" width="180"></el-table-column>
                        <el-table-column prop="updateTime" label="操作" width="180">
                            <el-button type="primary" size="small" @click="dialogFormVisible1 = true">修改</el-button>
                            
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>

        <el-dialog title="新增学生" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="学号" :label-width="formLabelWidth">
                    <el-input v-model="form.sno" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.sname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" :label-width="formLabelWidth">
                    <el-input v-model="form.sage" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addStudent">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="修改学生" :visible.sync="dialogFormVisible1">
            <el-form :model="form">
                <el-form-item label="学号" :label-width="formLabelWidth">
                    <el-input v-model="form.sno" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="form.sname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" :label-width="formLabelWidth">
                    <el-input v-model="form.sage" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="">确 定</el-button>
            </div>
        </el-dialog>



    </div>
</template>

<script>
export default {
    data() {
        return {
            dialogFormVisible: false,
            dialogFormVisible1: false,
            form: {
            },
            formLabelWidth: '120px',


            students: []
        }
    },
    created() {
        this.fetchStudents()
    },
    methods: {
        addStudent() {
            this.$axios.post('http://localhost:8080/student/studentInser', this.form
                , {
                    headers: {
                        'token': localStorage.getItem('token'),
                        'Content-Type': 'application/json'
                    }
                }
            )
                .then(response => {
                    console.log('response', response);
                    if (response.data == null || response.data == '') {
                        this.$message.error('新增失败');
                    } else {
                        this.$message.success('新增成功');
                        this.dialogFormVisible = false
                        this.fetchStudents()
                    }
                })
                .catch(error => {
                    console.log('error', error);
                });

        },
        exit() {
            localStorage.removeItem('token'); // 删除缓存
            this.$router.replace('/');
        },
        toData1() {
            // 跳转到学生管理页面
            this.$router.push('/data1');
        },

        fetchStudents() {
            this.$axios.get('http://localhost:8080/student/studentInfolist',
                {
                    headers: {
                        'token': localStorage.getItem('token'),
                        'Content-Type': 'application/json'
                    }
                })
                .then(response => {
                    console.log('response', response);
                    if (response.data == null || response.data == '') {
                        this.$message.error('获取数据失败');
                    } else {
                        console.log('111111');
                        this.students = response.data
                        this.$message.success('获取数据成功');
                    }
                })
                .catch(error => {
                    console.log('error', error);
                });
        },
    },
    mounted: function () {
        console.log('mounted');
        // this.fetchStudents()
    },
}
</script>
<style scoped>
.a0 {
    display: flex;
    flex-direction: row;
    height: 100vh;
}

.left {
    display: flex;
    flex-direction: column;
    width: 200px;
    height: 100%;
    background-color: aquamarine;
}

.a1 {
    margin-top: 20px;
}
</style>